package data

import (
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
)

type CarrierOwnerDto struct {
	Id                int    `orm:"pk;column(id)"`
	Stars             int    `orm:"column(stars)"`
	VehicleType       int    `orm:"column(vehicle_type)"`
	VehicleLength     int    `orm:"column(vehicle_len)"`
	VehicleLoad       int    `orm:"column(vehicle_load)"`
	VehicleNo         string `orm:"column(vehicle_no)"`
	FrequentLineStart string `orm:"column(frequent_line_start)"`
	FrequentLineEnd   string `orm:"column(frequent_line_end)"`
	ShipName          string `orm:"column(ship_name)"`
	ShipMMSI          string `orm:"column(ship_mmsi)"`
	ShipExDate        string `orm:"column(ship_ex_date)"`
	ShipHarbour       string `orm:"column(ship_harbour)"`
	ShipWidth         string `orm:"column(ship_width)"`
	ShipDepth         string `orm:"column(ship_depth)"`
	ShipCapacity      string `orm:"column(ship_capacity)"`
	ShipFullLoad      string `orm:"column(ship_full_load)"`
	ShipEmptyLoad     string `orm:"column(ship_empty_load)"`
	ShipHatchway      string `orm:"column(ship_hatchway)"`
}

type CarrierOwnerGetDto struct {
	Name               string `orm:"column(name)"`
	Stars              int    `orm:"column(stars)"`
	Phone              string `orm:"column(phone)"`
	Photo              string `orm:"column(photo)"`
	IDCardNo           string `orm:"column(id_card_no)"`
	VehicleType        int    `orm:"column(vehicle_type)"`
	VehcileLen         int    `orm:"column(vehicle_len)"`
	VehicleLoad        int    `orm:"column(vehicle_load)"`
	VehicleNo          string `orm:"column(vehicle_no)"`
	VehcilePhotoUrl    string `orm:"column(vehicle_photo)"`
	DrivingLicenseUrl  string `orm:"column(driving_license)"`
	VehicleLicenseUrl1 string `orm:"column(vehicle_license_1)"`
	VehicleLicenseUrl2 string `orm:"column(vehicle_license_2)"`
	FrequentLineStart  string `orm:"column(frequent_line_start)"`
	FrequentLineEnd    string `orm:"column(frequent_line_end)"`
	ScoreTotal         int    `orm:"column(score_total)"`
	ScoreCount         int    `orm:"column(score_count)"`
	CreateTime         string `orm:"column(create_time)"`
	ShipName           string `orm:"column(ship_name)"`
	ShipMMSI           string `orm:"column(ship_mmsi)"`
	ShipExDate         string `orm:"column(ship_ex_date)"`
	ShipHarbour        string `orm:"column(ship_harbour)"`
	ShipWidth          string `orm:"column(ship_width)"`
	ShipDepth          string `orm:"column(ship_depth)"`
	ShipCapacity       string `orm:"column(ship_capacity)"`
	ShipFullLoad       string `orm:"column(ship_full_load)"`
	ShipEmptyLoad      string `orm:"column(ship_empty_load)"`
	ShipHatchway       string `orm:"column(ship_hatchway)"`
}

func init() {
	orm.RegisterModel(new(CarrierOwnerDto))
}

func (c *CarrierOwnerDto) TableName() string {
	return "m_carrier_owner"
}

func (r MemberRepo) UpdateCarrierOwner(dto *CarrierOwnerDto) (err error) {
	var mbrType string
	mbr := &MemberDto{Id: dto.Id, TypeId: 2, AuthState: 1}
	o := orm.NewOrm()
	if _, err = o.Update(dto); err == nil {
		if mbrType, err = r.GetMemberType(dto.Id); err == nil {
			o.Update(mbr, "AuthState")
			if mbrType == "0" {
				_, err = o.Update(mbr, "TypeId")
			}
		}
	}
	return
}

func (r MemberRepo) GetCarrierOwner(id int) (dto CarrierOwnerGetDto, err error) {
	o := orm.NewOrm()
	dto = CarrierOwnerGetDto{}
	err = o.Raw(SQL_Mbr_Get_Carrier_Owner, id).QueryRow(&dto)
	return
}

func (r MemberRepo) EnsureCarrierOwner(id int) bool {
	var list orm.ParamsList
	o := orm.NewOrm()
	num, _ := o.Raw(SQL_Mbr_Ensure_Carrier_Owner, id).ValuesFlat(&list)
	return num == 1
}
